<?php

namespace App\Http\Controllers;

use App\Events\OrderPaid;
use App\Events\RechargeComplete;
use App\Models\Order;
use App\Models\PaylogModel;
use App\Models\Product;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class TestController extends Controller
{
    public function create_payment()
    {
        $reference = [
            "amount"        => "9701.84",
            "end_datetime"  => "2021-12-31",
            "custom_fields" => [
              "invoice" => "2018/0333"
            ]
        ];

        $data = json_encode($reference);

        $curl = curl_init();

        $httpHeader = [
            "Authorization: " . "Token " . "im5lqr34fwt37vougnpe4nuizu6exzlf",
            "Accept: application/vnd.proxypay.v2+json",
            "Content-Type: application/json",
            "Content-Length: " . strlen($data)
        ];

        $opts = [
            CURLOPT_URL             => "https://api.sandbox.proxypay.co.ao/references/904800000",
            CURLOPT_CUSTOMREQUEST   => "PUT",
            CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_HTTPHEADER      => $httpHeader,
            CURLOPT_POSTFIELDS      => $data
        ];

        curl_setopt_array($curl, $opts);

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);
    }

    public function payments()
    {
        $curl = curl_init();

        $httpHeader = [
            "Authorization: " . "Token " . "im5lqr34fwt37vougnpe4nuizu6exzlf",
            "Accept: application/vnd.proxypay.v2+json",
        ];

        $opts = [
            CURLOPT_URL             => "https://api.sandbox.proxypay.co.ao/payments",
            CURLOPT_CUSTOMREQUEST   => "GET",
            CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_HTTPHEADER      => $httpHeader
        ];

        curl_setopt_array($curl, $opts);

        $response = curl_exec($curl);
        $err = curl_error($curl);

        dd(json_decode($response), $err);
        curl_close($curl);
    }

    public function mock_payment()
    {
        $payment = [
            "reference_id"  => 574850000,
            "amount"        => "380.44"
        ];

        $data = json_encode($payment);

        $curl = curl_init();

        $httpHeader = [
            "Authorization: " . "Token " . "im5lqr34fwt37vougnpe4nuizu6exzlf",
            "Accept: application/vnd.proxypay.v2+json",
            "Content-Type: application/json",
            "Content-Length: " . strlen($data)
        ];

        $opts = [
            CURLOPT_URL             => "https://api.sandbox.proxypay.co.ao/payments",
            CURLOPT_CUSTOMREQUEST   => "POST",
            CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_HTTPHEADER      => $httpHeader,
            CURLOPT_POSTFIELDS      => $data
        ];

        curl_setopt_array($curl, $opts);

        $response = curl_exec($curl);
        $err = curl_error($curl);

        dd($response, $err);

        curl_close($curl);
    }

    public function reference_ids()
    {
        $curl = curl_init();

        $httpHeader = [
            "Authorization: " . "Token " . "im5lqr34fwt37vougnpe4nuizu6exzlf",
            "Accept: application/vnd.proxypay.v2+json",
        ];

        $opts = [
            CURLOPT_URL             => "https://api.sandbox.proxypay.co.ao/reference_ids",
            CURLOPT_CUSTOMREQUEST   => "POST",
            CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_HTTPHEADER      => $httpHeader
        ];

        curl_setopt_array($curl, $opts);

        $response = curl_exec($curl);

        dd($response);
        $err = curl_error($curl);

        curl_close($curl);
    }


    public function product()
    {
        $product_list = Product::where('pid', 0)->get();
        foreach ($product_list as $k => $p) {
            $product_stock = $p->stocks->first();
            if($p->unit_price != $product_stock->price) {
//                var_dump($p->id);
                $product_stock->fill(['price' => $p->unit_price])->save();
            }
        }

    }


    /**
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     */
    public function index()
    {
        $user_list = User::all();
        foreach ($user_list as $v) {
            if(!$v->referral_code) {
                $v->fill(['referral_code' => Str::random(12)])->save();
            }
            $v->node = empty($v->node) ? $v->id : $v->node;
            $v->save();
            $temp_arr = explode(',', $v->node);
            if($temp_arr[0] == $v->id) {
                $v->fill(['referred_by' => 0, 'deep' => count($temp_arr) - 1])->save();
            } else {
                $v->fill(['referred_by' => $temp_arr[0], 'deep' => count($temp_arr) - 1])->save();
            }
        }

    }
}
